//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
texture g_txScene : TEXTURE;
float4x4 g_mWorld : WORLD;
float4x4 g_mView : VIEW;
float4x4 g_mProj : PROJECTION;

#define g_nNumBatchInstance 128
float4 g_vPositions[g_nNumBatchInstance] : BOXINSTANCEARRAY_POSITION;

struct v2p
{
	float4 pos : POSITION;
	float2 Tex : TEXCOORD0;
	float light : TEXCOORD1;
};

//-----------------------------------------------------------------------------
// Texture sampler
//-----------------------------------------------------------------------------
sampler g_samScene =
sampler_state
{
    Texture = <g_txScene>;
	AddressU = Clamp;
	AddressV = Clamp;

    MinFilter = None;
    MagFilter = None;
    MipFilter = None;
};

v2p VS_ShaderInstancing(float4 vPos : POSITION, float2 vTex0 : TEXCOORD0, float index : TEXCOORD1)
{
	v2p outVS;
	outVS.pos = float4(g_vPositions[index].xyz, 1.f) + vPos;
	
	// Transform the position from object space to homogeneous projection space
	outVS.pos = mul(outVS.pos, g_mWorld);
	outVS.pos = mul(outVS.pos, g_mView);
	outVS.pos = mul(outVS.pos, g_mProj);
	
	// Just copy the texture coordinate through
	outVS.Tex = vTex0;
	outVS.light = g_vPositions[index].w;
	return outVS;
}

float4 PS(v2p inPS) : COLOR0
{
    // Lookup texture and modulate it with diffuse
	float4 texColor = tex2D(g_samScene, inPS.Tex);
    //return float4(texColor.rgb * 1.f, texColor.a);
    return float4(texColor.rgb * inPS.light, texColor.a);
}


//-----------------------------------------------------------------------------
// Name: TShader_Instancing
// Type: Technique
// Desc: Renders scene through Shader instancing (batching)
//-----------------------------------------------------------------------------
technique TShader_Instancing
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS_ShaderInstancing();
        PixelShader  = compile ps_2_0 PS();
        AlphaBlendEnable = true;
		AlphaTestEnable = true;
		CullMode = CCW;
    }
}

technique TShader_Instancing_Alpha
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS_ShaderInstancing();
        PixelShader  = compile ps_2_0 PS();
        AlphaBlendEnable = true;
		AlphaTestEnable = true;
		CullMode = CCW;
    }
}
